<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">论文管理系统</div>
      <div class="login-register">
        <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
          <!--登录-->
          <el-tab-pane id="login" label="登录" name="first">
            <el-form
                ref="loginForm"
                :model="loginForm"
                :rules="rules"
                label-width="80px"
                class="login"
                @keyup.enter.native="submitForm('loginForm')"
            >
              <el-form-item label="帐号" prop="email">
                <el-input v-model="loginForm.email"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="loginForm.password" show-password></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
                <el-button @click="resetForm('loginForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!--注册-->
          <el-tab-pane label="注册" name="second">

          </el-tab-pane>

        </el-tabs>
      </div>


    </div>
  </div>

</template>

<script>
import qs from "qs";

export default {
  data() {
    return {

      loginForm: {
        email: "",
        password: "",
      },
      rules: {
        email: [
          {
            required: true,
            message: "Please input email address",
            trigger: "blur",
          },
          {
            type: "email",
            message: "Please input correct email address",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            required: true,
            message: "Please input password",
            trigger: "blur",
          },
          {
            min: 8,
            max: 30,
            message: "Length should be 8 to 30",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // axios
          //   .post("https://partner.apptutti.com/api/partner/login",
          this.$http
              .get("/user/loginByEmail", {
                email: JSON.stringify(this.loginForm.email),
                password: JSON.stringify(this.loginForm.password)
              })
              .then((res) => {
                res = res.data;
                // console.log(res);
                if (res.code === 200) {
                  localStorage.setItem("Token", res.data.token);
                  this.$message.success("Login successfully! Welcome!");
                  this.$router.push("/");
                } else {
                  alert(res.msg);
                  this.$message.error("Oops, error submit.");
                  if (res.code === 401) {
                    localStorage.removeItem("Token");
                    this.$router.push("/login");
                  }
                }
              });
        } else {
          this.$message.error("Oops, error submit.");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../assets/img/book.png);
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 38%;
  top: 30%;
  width: 50%;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 50%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
.login-register{
  position: relative;
  width: 97%;
  left: 1.5%;
}

.login {
  width: 500px;
  background-color: #ffffff;
  border-radius: 10px;
  margin: 50px auto;
  padding: 50px;
  box-shadow: 10px 10px 40px 0px rgba(0, 0, 0, 0.1);
}
h1 {
  color: var(--el-color-primary);
}
</style>
